<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox
        v-for="item in list"
        :key="item.id"
        :label="item.id"
      >{{ item.name }}</el-checkbox>

    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRole } from '@/api/settings'
import { getInfo } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    codeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 与多选框进行绑定的数据
      list: []
    }
  },
  created() {
    this.lodeRole()
  },
  methods: {

    // 分配角色
    async   hSubmit() {
      try {
        const res = await assignRoles({ id: this.codeId, roleIds: this.roleIds })
        console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 通知父组件关闭弹层
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },

    // 获取角色列表
    async lodeRole() {
      try {
        const res = await getRole({ page: 1, pagesize: 100 })
        console.log('getRole', res)
        // 保存数据
        if (res.code === 10000) {
          this.$message.success(res.message)
          this.list = res.data.rows
        }
        // 发送api/user 里面的获取用户的详细信息的请求 获取角色
        const defLits = await getInfo(this.codeId)
        console.log(defLits)
        this.roleIds = defLits.data.roleIds
      } catch (err) {
        console.log('getRole', err)
      }
    },
    closeDialog() {

    }
  }
}
</script>
